{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read <Response [200]> from https://www.baidu.com/s?wd=周杰伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周饼伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周口伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=黑伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=粥饼伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=雍杰伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=孙冬\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周口同学\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=上海杰伦老师\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=方文山\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=马云\n",
      "Download 11 sites in 14.49426940000194 seconds\n"
     ]
    }
   ],
   "source": [
    "import requests\n",
    "import time\n",
    "url = ''\n",
    "headers= {\n",
    "'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'\n",
    "}\n",
    "def download_one(url):\n",
    "    #resp = requests.get(url)\n",
    "    resp = requests.get(url=url,headers=headers)\n",
    "    print('Read {} from {}'.format(resp,url))\n",
    "def download_all(sites):\n",
    "    for site in sites:\n",
    "        download_one(site)\n",
    "def main():\n",
    "    sites = [\n",
    "        'https://www.baidu.com/s?wd=周杰伦',\n",
    "        'https://www.baidu.com/s?wd=周饼伦',\n",
    "        'https://www.baidu.com/s?wd=周口伦',\n",
    "        'https://www.baidu.com/s?wd=黑伦',\n",
    "        'https://www.baidu.com/s?wd=粥饼伦',\n",
    "        'https://www.baidu.com/s?wd=雍杰伦',\n",
    "        'https://www.baidu.com/s?wd=孙冬',\n",
    "        'https://www.baidu.com/s?wd=周口同学',\n",
    "        'https://www.baidu.com/s?wd=上海杰伦老师',\n",
    "        'https://www.baidu.com/s?wd=方文山',\n",
    "        'https://www.baidu.com/s?wd=马云',\n",
    "    ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read <Response [200]> from https://www.baidu.com/s?wd=粥饼伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周饼伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=黑伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周口伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周杰伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周口同学\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=雍杰伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=上海杰伦老师\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=孙冬\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=方文山\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=马云\n",
      "Download 11 sites in 4.634771699998964 seconds\n"
     ]
    }
   ],
   "source": [
    "# 多线程读取数据版本\n",
    "import requests\n",
    "import time\n",
    "import concurrent.futures\n",
    "headers= {\n",
    "'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'\n",
    "}\n",
    "def download_one(url):\n",
    "    #resp = requests.get(url)\n",
    "    resp = requests.get(url=url,headers=headers)\n",
    "    print('Read {} from {}'.format(resp,url))\n",
    "def download_all(sites):\n",
    "    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:\n",
    "            executor.map(download_one,sites)\n",
    "    # for site in sites:\n",
    "        \n",
    "            #download_one(site)\n",
    "def main():\n",
    "    sites = [\n",
    "        'https://www.baidu.com/s?wd=周杰伦',\n",
    "        'https://www.baidu.com/s?wd=周饼伦',\n",
    "        'https://www.baidu.com/s?wd=周口伦',\n",
    "        'https://www.baidu.com/s?wd=黑伦',\n",
    "        'https://www.baidu.com/s?wd=粥饼伦',\n",
    "        'https://www.baidu.com/s?wd=雍杰伦',\n",
    "        'https://www.baidu.com/s?wd=孙冬',\n",
    "        'https://www.baidu.com/s?wd=周口同学',\n",
    "        'https://www.baidu.com/s?wd=上海杰伦老师',\n",
    "        'https://www.baidu.com/s?wd=方文山',\n",
    "        'https://www.baidu.com/s?wd=马云',\n",
    "    ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Download 11 sites in 0.5404649999982212 seconds\n"
     ]
    }
   ],
   "source": [
    "# 多进程读取数据版本\n",
    "import requests\n",
    "import time\n",
    "import concurrent.futures\n",
    "headers= {\n",
    "'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'\n",
    "}\n",
    "def download_one(url):\n",
    "    #resp = requests.get(url)\n",
    "    resp = requests.get(url=url,headers=headers)\n",
    "    print('Read {} from {}'.format(resp,url))\n",
    "def download_all(sites):\n",
    "    with concurrent.futures.ProcessPoolExecutor() as executor:\n",
    "            executor.map(download_one,sites)\n",
    "    # for site in sites:\n",
    "        \n",
    "            #download_one(site)\n",
    "def main():\n",
    "    sites = [\n",
    "        'https://www.baidu.com/s?wd=周杰伦',\n",
    "        'https://www.baidu.com/s?wd=周饼伦',\n",
    "        'https://www.baidu.com/s?wd=周口伦',\n",
    "        'https://www.baidu.com/s?wd=黑伦',\n",
    "        'https://www.baidu.com/s?wd=粥饼伦',\n",
    "        'https://www.baidu.com/s?wd=雍杰伦',\n",
    "        'https://www.baidu.com/s?wd=孙冬',\n",
    "        'https://www.baidu.com/s?wd=周口同学',\n",
    "        'https://www.baidu.com/s?wd=上海杰伦老师',\n",
    "        'https://www.baidu.com/s?wd=方文山',\n",
    "        'https://www.baidu.com/s?wd=马云',\n",
    "    ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Read <Response [200]> from https://www.baidu.com/s?wd=黑伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周饼伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周杰伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周口伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=粥饼伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=孙冬\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=雍杰伦\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=上海杰伦老师\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=周口同学\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=方文山\n",
      "Read <Response [200]> from https://www.baidu.com/s?wd=马云\n",
      "Download 11 sites in 4.893995800000994 seconds\n"
     ]
    }
   ],
   "source": [
    "# 多线程读取数据版本\n",
    "import requests\n",
    "import time\n",
    "import concurrent.futures\n",
    "headers= {\n",
    "'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36 Edg/123.0.0.0'\n",
    "}\n",
    "def download_one(url):\n",
    "    #resp = requests.get(url)\n",
    "    resp = requests.get(url=url,headers=headers)\n",
    "    print('Read {} from {}'.format(resp,url))\n",
    "def download_all(sites):\n",
    "    with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:\n",
    "        to_do = []\n",
    "        for site in sites:\n",
    "            future = executor.submit(download_one,site)\n",
    "            to_do.append(future)\n",
    "        for future in concurrent.futures.as_completed(to_do):\n",
    "            future.result()  # 获取返回值\n",
    "        \n",
    "def main():\n",
    "    sites = [\n",
    "        'https://www.baidu.com/s?wd=周杰伦',\n",
    "        'https://www.baidu.com/s?wd=周饼伦',\n",
    "        'https://www.baidu.com/s?wd=周口伦',\n",
    "        'https://www.baidu.com/s?wd=黑伦',\n",
    "        'https://www.baidu.com/s?wd=粥饼伦',\n",
    "        'https://www.baidu.com/s?wd=雍杰伦',\n",
    "        'https://www.baidu.com/s?wd=孙冬',\n",
    "        'https://www.baidu.com/s?wd=周口同学',\n",
    "        'https://www.baidu.com/s?wd=上海杰伦老师',\n",
    "        'https://www.baidu.com/s?wd=方文山',\n",
    "        'https://www.baidu.com/s?wd=马云',\n",
    "    ]\n",
    "    start_time = time.perf_counter()\n",
    "    download_all(sites)\n",
    "    end_time = time.perf_counter()\n",
    "    print('Download {} sites in {} seconds'.format(len(sites), end_time - start_time))\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
